package com.k.liuzh.extraffic;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.JsonArray;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Method;

import static com.android.volley.Request.Method.GET;
import static com.android.volley.Request.Method.POST;

public class LoginActivity extends AppCompatActivity {
    private Button button;
    private EditText editText1;
    private EditText editText2;
    private static final String TAG = "LoginActivity";
    private SharedPreferences.Editor editor;
    private SharedPreferences sharedPreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        button = findViewById(R.id.login_denglu);
        editText1 = findViewById(R.id.login_userName);
        editText2 = findViewById(R.id.login_userPasswd);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                editor = getSharedPreferences("login",MODE_PRIVATE).edit();
                sharedPreferences = getSharedPreferences("login",MODE_PRIVATE);
                final String name = editText1.getText().toString();
                final String passwd = editText2.getText().toString();
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("UserName",name);
                    jsonObject.put("UserPwd",passwd);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                String url = "http://47.106.153.72:8081/transportservice/action/user_login.do";
                RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
                JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(POST, url, jsonObject,
                        new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        String result = jsonObject.optString("RESULT");
                        Log.d(TAG, "onResponse: result:" + result);
                        if("S".equals(result)){
                            editor.putString("userName",name);
                            intent.putExtra("userName",name);
                            startActivity(intent);
                            editor.putBoolean("isSuccess",true);
                            editor.commit();
                            Boolean isTrue = sharedPreferences.getBoolean("isSuccess",false);
                            Log.d(TAG, "onResponse: "+isTrue);
                            finish();
                        }else {
                            Toast.makeText(getApplicationContext(),"账号或密码错误",Toast.LENGTH_SHORT).show();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Toast.makeText(getApplicationContext(),"网络错误",Toast.LENGTH_SHORT).show();
                    }
                });


                requestQueue.add(jsonObjectRequest);
            }
        });


    }
}
